<?php
defined('IN_NWIDC') or exit('Access Invalid!');
header("content-Type: text/html; charset=UTF-8");

class zhifuqr{
    /*
     * 商户号
     * */
    private $merchant_code       = '';
    /*
     * 商户私钥
     * */
    private $merchant_private_key = '';
    /*
     * 商户公钥
     * */
    private $merchant_public_key  = '';
    /*
     * 智付公钥
     * */
    private $dinpay_public_key    = '';
    /**
     * 支付结果
     * return boolean
     */
    private $pay_result           = false;
    /*
     * 接口配置信息(array)
     * */
    private $request              = array();
    /*
     * 订单信息(array)
     * */
    private $order                = array();

    public function __construct($payment_info,$order_info){
        $this->zhifubqr($payment_info,$order_info);
    }
    public function zhifubqr($payment_info = array(),$order_info = array()){
        if(!empty($payment_info) and !empty($order_info)){

            $this->request	            = $payment_info;
            $this->order	            = $order_info;

            $this->merchant_code        = $this->request['payment_config']['merchant_code'];
            $this->merchant_private_key = preg_replace('/\s*/', '', trim($this->request['payment_config']['private_key']));
            $this->merchant_public_key  = preg_replace('/\s*/', '', trim($this->request['payment_config']['public_key']));
            $this->dinpay_public_key    = preg_replace('/\s*/', '', trim($this->request['payment_config']['dinpay_public_key']));
        }
    }


    public function get_payurl(){
        $out_trade_no = $this->order['pay_sn'];
        $order_type = $this->order['order_type'];
        $hash = md5($out_trade_no.$order_type);
        $reqUrl = "/shop/index.php?act=zhifuqr&op=zhifuqr&trade_no=$out_trade_no"."&order_type=".$order_type."&hash=".$hash;
        return $reqUrl;
    }


    public function notify_verify(){

        $data_arr = explode('__',$_POST['extra_return_param']);

        if($data_arr[0] != $this->order['order_type']){
            exit('参数异常');
        }

        $parms = array(
            'merchant_code'	       => $_POST["merchant_code"],
            'interface_version'    => $_POST["interface_version"],
            'sign_type'            => $_POST["sign_type"],
            'dinpaySign'           => base64_decode($_POST["sign"]),
            'notify_type'          => $_POST["notify_type"],
            'notify_id'            => $_POST["notify_id"],
            'order_no'             => $_POST["order_no"],
            'order_time'           => $_POST["order_time"],
            'order_amount'         => $_POST["order_amount"],
            'trade_status'         => $_POST["trade_status"],
            'trade_time'           => $_POST["trade_time"],
            'trade_no'             => $_POST["trade_no"],
            'extra_return_param'   => $_POST["extra_return_param"],
            'bank_seq_no'          => $_POST["bank_seq_no"]
        );



/////////////////////////////   参数组装  /////////////////////////////////
        /**
        除了sign_type dinpaySign参数，其他非空参数都要参与组装，组装顺序是按照a~z的顺序，下划线"_"优先于字母
         */


        $signStr = "";

        if($parms['bank_seq_no'] != ""){
            $signStr = $signStr."bank_seq_no=".$parms['bank_seq_no']."&";
        }

        if($parms['extra_return_param'] != ""){
            $signStr = $signStr."extra_return_param=".$parms['extra_return_param']."&";
        }
        $signStr = $signStr."interface_version=".$parms['interface_version']."&";
        $signStr = $signStr."merchant_code=".$parms['merchant_code']."&";
        $signStr = $signStr."notify_id=".$parms['notify_id']."&";
        $signStr = $signStr."notify_type=".$parms['notify_type']."&";
        $signStr = $signStr."order_amount=".$parms['order_amount']."&";
        $signStr = $signStr."order_no=".$parms['order_no']."&";
        $signStr = $signStr."order_time=".$parms['order_time']."&";
        $signStr = $signStr."trade_no=".$parms['trade_no']."&";
        $signStr = $signStr."trade_status=".$parms['trade_status']."&";
        $signStr = $signStr."trade_time=".$parms['trade_time'];

        //echo $signStr;

/////////////////////////////   RSA-S验证  /////////////////////////////////

        $dinpay_public_key = "-----BEGIN PUBLIC KEY-----"."\r\n".wordwrap(trim($this->dinpay_public_key),62,"\r\n",true)."\r\n"."-----END PUBLIC KEY-----";

        $dinpay_public_key = openssl_get_publickey($dinpay_public_key);

        $flag = openssl_verify($signStr,$parms['dinpaySign'],$dinpay_public_key,OPENSSL_ALGO_MD5);

///////////////////////////   响应“SUC CESS” /////////////////////////////

        if($flag){
            // echo"SUCCESS";
            if($_POST['trade_status']=='SUCCESS'){
                $this->pay_result = true;
                echo "SUCCESS";
                return true;
            }else{
                return false;
            }

        }else{
            //echo"Verification Error";or";
            return false;
        }

    }

    public function return_verify(){
        return $this->notify_verify();
    }

    /*
     * 取得订单支付状态，成功或失败
     * */
    public function getPayResult($param){
        return $this->pay_result;
    }
}

